package com.printeverywhere.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class NutchUtil {

	public static void writeToFile(String[] urlName, String fileName) {
		try {
			// Create file
			FileWriter fstream = new FileWriter(fileName);
			BufferedWriter out = new BufferedWriter(fstream);
			for (int index = 0; index < urlName.length; index++) {
				out.write(urlName[index]);
				out.write(AdminConstants.NEW_LINE);
			}
			// Close the output stream
			out.close();
		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}
	public static void editFileByLocation(String filePath, String addHomePageUrl) throws IOException{
			try {
				File file = new File(filePath);
				FileReader fr = new FileReader(file);

				BufferedReader in = new BufferedReader(fr);
				String h=in.readLine() ;
				List<String> l = new ArrayList<String>();
				l.add(h);
				while(h != null){
					if(h.equalsIgnoreCase("# skip everything else")){
						break;
					}
					l.add(h);
					h=in.readLine();
				}
				file.delete();
				PrintWriter writer=new PrintWriter(file);
				for (Iterator iterator = l.iterator(); iterator.hasNext();) {
				String st = (String) iterator.next();
				writer.append("\n");
				writer.append(st);

			}
				//Add name of urls.
				writer.append("+^http://([a-z0-9]*\\.)*");
				writer.append(addHomePageUrl);
				writer.append("/");
				writer.append("\n");
				writer.append("\n");
				writer.append("# skip everything else");
				writer.append("\n");
				writer.append("-.");
				writer.append("\n");
				writer.flush();
				writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		}

	public static void main(String[] args) {

	}
}
